package com.haois.ws.web.module.navigate.rest;

import com.haois.ws.common.constant.Reply;
import com.haois.ws.common.constant.ReturnState;
import com.haois.ws.web.module.navigate.service.WebSiteService;
import com.haois.ws.common.util.Identities;
import com.haois.ws.common.util.MD5Coder;
import com.haois.ws.common.util.RespUtil;
import com.haois.ws.web.module.navigate.daomain.WebSite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by RuiHaoZ on 2018/3/11 10:55
 */
@RestController
@RequestMapping("/website")
public class WebSiteController {

    private Logger logger = LoggerFactory.getLogger(WebSiteController.class);

    @Autowired
    private WebSiteService webSiteService;

    /**
     * @api {GET} /website/list 获取网站列表
     * @apiGroup webSite
     * @apiName getAllWebSite
     * @apiDescription 获取全部网站列表
     * @apiParamExample {json} 请求样例：
     * GET /website/list
     * @apiSuccessExample {json} 返回样例:
     * {"code":0,"msg":"success","data":[{"id":45,"soft":"home","count":18},{"id":46,"soft":"JustPhoneBook","count":148}]}
     */
    @RequestMapping(value = "/list")
    public String getAllWebSite(HttpServletResponse response) {
        RespUtil.setACAO(response);
        List<WebSite> app = webSiteService.getAllApp();
        Reply<List<WebSite>> reply = new Reply<List<WebSite>>(ReturnState.CODE_00, "success", app);
        return reply.toJson();
    }

    /**
     * @api {GET} /website/see/{appId} 根据appId获取网站详情
     * @apiGroup webSite
     * @apiName webSite
     * @apiDescription 根据网站名获取该网页的访问次数
     * @apiParam {String} appId 网站Id
     * @apiParamExample {json} 请求样例：
     * GET /website/see/{appId}
     * @apiSuccessExample {json} 返回样例:
     * {"code":0,"msg":"success","data":18}
     */
    @RequestMapping(value = "/see/{appId}")
    public String webSite(@PathVariable String appId, HttpServletResponse response) {
        RespUtil.setACAO(response);
        WebSite webSite = webSiteService.getByAppId(appId);
        Reply reply = new Reply<WebSite>(ReturnState.CODE_00, "success", webSite);
        return reply.dataIsEmpty().toJson();
    }
    /**
     * @api {GET} /website/add 增加新的网站
     * @apiGroup webSite
     * @apiName recordCount
     * @apiDescription 增加新的网站
     * @apiParam {String} account 账户名
     * @apiParam {String} webName 网站名
     * @apiParamExample {json} 请求样例：
     * GET /website/add
     * @apiSuccessExample {json} 返回样例:
     * {"code":0,"msg":"success","data":18}
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST,params = {"account","webName"})
    public String add(WebSite app, HttpServletResponse response) {
        RespUtil.setACAO(response);
        String appId = MD5Coder.encodeCombine(app.getAccount(),app.getWebName());
        String appSecret = Identities.uuid2();
        app.setAppId(appId);
        app.setAppSecret(appSecret);
        List<WebSite> existApp = webSiteService.getByAccountAndWebName(app.getAccount(),app.getWebName());
        if (!existApp.isEmpty()){
                return new Reply(ReturnState.CODE_10, "success", "该网站已经存在").toJson();
        }
        webSiteService.addApp(app);
        return new Reply<WebSite>(ReturnState.CODE_00, "success", app).toJson();
    }


    /**
     * @api {GET} /website/list/{account} 根据帐户名获取网站列表
     * @apiGroup webSite
     * @apiName webSite
     * @apiDescription 根据网站名获取该网页的访问次数
     * @apiParam {String} soft 网站名
     * @apiParamExample {json} 请求样例：
     * GET /website/list/{account}
     * @apiSuccessExample {json} 返回样例:
     * {"code":0,"msg":"success","data":18}
     */
    @RequestMapping(value = "/list/{account}")
    public String getByAccount(@PathVariable String account, HttpServletResponse response) {
        RespUtil.setACAO(response);
        List<WebSite> app = webSiteService.getByAccount(account);
        Reply<List<WebSite>> reply = new Reply<List<WebSite>>(ReturnState.CODE_00, "success", app);
        return reply.toJson();
    }
}
